package org.hxyjs.service.impl;

import org.hxyjs.dao.MongodbDao;
import org.hxyjs.dao.MemberTypeDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;

import org.hxyjs.entity.Account;
import org.hxyjs.entity.MemberType;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.AccountService;
import org.hxyjs.service.MemberTypeService;
import org.hxyjs.vo.MemberTypeView;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

@Service("memberTypeService")
public class MemberTypeServiceImpl implements MemberTypeService, AccountService {
    @Resource
    MemberTypeDao memberTypeDao;
    @Resource
    MongodbDao mongodbDao;

    @Override
    public DTO<MemberType> getAllMemberTypes() throws Exception {
        List<MemberType> memberTypes = memberTypeDao.getAllMemberTypes();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(memberTypes).isPresent() ? MessageEnum.GETMEMBERTYPESUCCESS : MessageEnum.GETMEMBERTYPEFAIL;
        return new DTO<MemberType>(messageEnum.getCode(), messageEnum.getMessage(), memberTypes);
    }

    @Override
    public DTO<MemberType> getMemberTypeByUno(String mtno) throws Exception {
        MemberType memberType = memberTypeDao.getMemberTypeByUno(mtno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(mtno).isPresent() ? MessageEnum.GETMEMBERTYPESUCCESS : MessageEnum.GETMEMBERTYPEFAIL;
        return new DTO<MemberType>(messageEnum.getCode(), messageEnum.getMessage(), memberType);
    }

    @Override
    public DTO<MemberType> deleteMemberType(String mtno) throws Exception {
        Integer result = memberTypeDao.deleteMemberType(mtno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.DELETEMEMBERTYPESUCCESS : MessageEnum.DELETEMEMBERTYPEFAIL;
        return new DTO<MemberType>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<MemberType> updateMemberType(MemberTypeView memberTypeView) throws Exception {
        MemberType memberType = dateFormat(memberTypeView);
        Integer result = memberTypeDao.updateMemberType(memberType);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEMEMBERTYPESUCCESS : MessageEnum.UPDATEMEMBERTYPEFAIL;
        return new DTO<MemberType>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<MemberType> addMemberTypeByUno(MemberType memberType) throws Exception {

        Integer result = memberTypeDao.addMemberTypeByUno(memberType);

        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.ADDMEMBERTYPESUCCESS : MessageEnum.ADDMEMBERTYPEFAIL;
        return new DTO<MemberType>(messageEnum.getCode(), messageEnum.getMessage(), memberType);

    }

    @Override
    public DTO<MemberType> getMemberTypesByPage(PageInfo pageInfo) throws Exception {
        List<MemberType> memberTypes = memberTypeDao.getMemberTypesByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(memberTypes).isPresent() ? MessageEnum.GETMEMBERTYPESUCCESS : MessageEnum.GETMEMBERTYPEFAIL;
        return new DTO<MemberType>(messageEnum.getCode(), messageEnum.getMessage(), memberTypes, pageInfo);
    }

    @Override
    public DTO<MemberType> getMemberTypesWithOrders(String mtno) throws Exception {
        return null;
    }

    /**
     * 查询MongoDB
     *
     * @param mtno
     * @return
     * @throws Exception
     */

    @Override
    public DTO<MemberType> activate(String mtno) throws Exception {
//        MemberType memberType = new MemberType();
//        memberType.setUno(mtno);
//        memberType.setUstate("已激活");
//        Integer result = memberTypeDao.updateMemberType(memberType);
//        //isPresent表示判断不为空成立则dto为？与：之间的对象
//        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
//        return new DTO<MemberType>(messageEnum.getCode(), messageEnum.getMessage());
        return null;
    }

    @Override
    public DTO<MemberType> lettory(String mtno,String giftName) throws Exception {
        MemberType memberType = memberTypeDao.getMemberTypeByUno(mtno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(mtno).isPresent() ? MessageEnum.GETUSERSUCCESS : MessageEnum.GETUSERFAIL;
        return new DTO<MemberType>(messageEnum.getCode(), messageEnum.getMessage(), memberType,giftName);
    }


    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
        DTO<Account> dto = new DTO<Account>();
//        Optional<Account> adminOP = Optional.ofNullable(memberTypeDao.getAccountByAccountName(accountName));
//        dto.setT(adminOP.get());
//        MessageEnum messageEnum = adminOP.isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
//        dto.setCode(messageEnum.getCode());
//        dto.setMsg(messageEnum.getMessage());
        return dto;
    }

    public MemberType dateFormat(MemberTypeView memberTypeView) throws Exception{
        MemberType memberType = new MemberType();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        memberType.setMtbegintime(sdf.parse(memberTypeView.getMtbegintime()));
        memberType.setMtdesc(memberTypeView.getMtdesc());
        memberType.setMtno(memberTypeView.getMtno());
        memberType.setMtendtime(sdf.parse(memberTypeView.getMtendtime()));
        memberType.setMttype(memberTypeView.getMttype());

        return memberType;


    }
}
